﻿(function ($, undefined) {
    $.widget("ui.sliderselect", {
        options: undefined,

        _create: function () {
            if (this.options != undefined)
                this._refreshValue();
        },

        destroy: function () { },

        _setOption: function (key, value) {

            if (key === "onSelect") {
                this.options.onSelect = value;
            } else if (key === "onStop") {
                this.options.onStop = value;
            } else {
                return;
            }
            $.Widget.prototype._setOption.apply(this, arguments);
        },

        _refreshValue: function () {
            var $this = this;
            var containerPai = this.element.parent();
            var slider = $('<div></div>')
            containerPai.append(slider);
            this.element.hide();
            var maximo = this.element.find('option').length - 1;
            var valor = 0; // this.options.valor ? this.options.valor : 0;
            slider.slider({
                orientation: "vertical",
                range: "min",
                min: 0,
                max: maximo,
                value: valor,
                slide: function (event, ui) {
                    if ($this.options.onSelect) {
                        var option = $($this.element.find('option').get(ui.value))
                        option.attr('selected', 'selected');
                        var valorRetorno = option.text();
                        $this.options.onSelect($this.element, valorRetorno, option);
                    }
                },
                stop: function (event, ui) {
                    if ($this.options.onStop) {
                        $this.options.onStop($this.element);
                    }
                }
            });

        }
    });

    $.extend($.ui.sliderselect, {
        version: "1.0.0"
    });

})(jQuery);